'use strict';
const base = require('../base/controllerBase');

class SettingsController extends base {
  async updateSalesSetting() {
    const { ctx, service } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' },
      };
      ctx.validate(model, body);
      delete body._id;
      const result = await ctx.app.dbs.updateOne('Setting', body);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }

  async findSalesSetting() {
    const { ctx } = this;
    try {
      const { body } = ctx.request;
      const model = {
        tenantCode: { type: 'string' }
      };
      ctx.validate(model, body);
      const { tenantCode } = body;
      let result = await ctx.app.dbs.findOne('Setting', { tenantCode });
      if (!result) {
        result = { mgrFeeRatio: 0.08, tenantCode };
        const res = await ctx.app.dbs.insert('Setting', result);
      }
      console.log('findSalesSetting', result);
      ctx.body = { status: 'ok', result };
      ctx.status = 200;
    } catch (e) {
      const _message = e && e.message ? e.message : this.ERROR_MSG;
      ctx.body = { status: 'error', message: _message, result: e };
      ctx.status = 200;
    }
  }


}

module.exports = SettingsController;
